package utilities.googleMap;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import utilities.json.JSONArray;
import utilities.json.JSONException;
import utilities.json.JSONObject;
import utilities.json.JSONReader;

/**
 *
 * @author lxn
 */
public class Directions {

    /**
     * 
     * @param from
     * @param to
     * @return
     */
    public static List<String> getDirections(String from, String to) {
        JSONArray steps;
        JSONObject reply, o;
        List<String> directions = new LinkedList<String>();
        String map, polyline, s, url;
        String f = null;
        String t = null;
        double[] sa = new double[2];
        double[] ea = new double[2];

        try {
            f = URLEncoder.encode(from, "UTF-8");
            t = URLEncoder.encode(to, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Directions.class.getName()).log(Level.SEVERE, null, ex);
        }

        url = "https://maps.googleapis.com/maps/api/directions/json?";
        url += "origin=" + f + "&";
        url += "destination=" + t + "&";
        url += "language=it&";
        url += "region=it&";
        url += "sensor=false";

        try {
            reply = JSONReader.readJSONFromUrl(url);

            if (!reply.getString("status").equals("OK")) {
                return directions;
            }

            o = reply.getJSONArray("routes").getJSONObject(0).
                    getJSONArray("legs").getJSONObject(0);

            sa[0] = o.getJSONObject("start_location").getDouble("lat");

            sa[1] = o.getJSONObject("start_location").getDouble("lng");

            ea[0] = o.getJSONObject("end_location").getDouble("lat");

            ea[1] = o.getJSONObject("end_location").getDouble("lng");

            polyline = reply.getJSONArray("routes").getJSONObject(0).
                    getJSONObject("overview_polyline").getString("points");

            map = "http://maps.googleapis.com/maps/api/staticmap?";
            map += "size=512x512&";
            map += "markers=color:green|label:A|" + sa[0] + "," + sa[1] + "&";
            map += "markers=color:green|label:B|" + ea[0] + "," + ea[1] + "&";
            map += "path=enc:" + polyline + "&";
            map += "sensor=false";

            directions.add(map);

            steps = o.getJSONArray("steps");

            for (int i = 0; i < steps.length(); i++) {
                s = steps.getJSONObject(i).getString("html_instructions");
                
                if (s.matches(".*</?div.*")) {
                    s = s.replaceAll("<div", ".<div");
                    s = s.replaceAll("</div", ".</div");
                } else {
                    s += ".";
                }
                
                directions.add(s);
            }
        } catch (IOException ex) {
            Logger.getLogger(Directions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JSONException ex) {
            Logger.getLogger(Directions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return directions;
    }

}
